package com.sunshine.composeapp

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.absolutePadding
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons.Filled
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MediumTopAppBar
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.integerResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sunshine.composeapp.bean.Message
import com.sunshine.composeapp.ui.theme.ComposeAppTheme

@ExperimentalMaterial3Api
class ProtectActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeAppTheme {
//                MyApp(modifier = Modifier.fillMaxSize())
                LargeTopAppBarExample()
            }
        }
    }

    @Composable
    fun Greeting(message: Message) {
        Row(modifier = Modifier.padding(all = 6.dp)) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "Image test", modifier = Modifier
                    .size(40.dp)
                    .clip(
                        CircleShape
                    )
            )
            Spacer(modifier = Modifier.width(8.dp))
            Column {
                Text(text = message.title)
                Spacer(modifier = Modifier.width(8.dp))
                Text(text = message.content)
            }
        }
    }

    @Composable
    fun Greeting(name: String) {
        var expanded by rememberSaveable {
            mutableStateOf(false)
        }
        val extraPadding by animateDpAsState(
            if (expanded) 48.dp else 0.dp
        )
        Card(
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.primary
            ), modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
        ) {
            Row(modifier = Modifier.padding(20.dp)) {
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .padding(bottom = extraPadding)
                ) {
                    Text(
                        text = "hello,", style = MaterialTheme.typography.headlineMedium.copy(
                            fontWeight = FontWeight.ExtraBold
                        )
                    )
                    Text(text = name)
                    if (expanded) {
                        Text(
                            text = ("Compose ipsum color sit lazy, " + "padding theme it, sed do bouncy. ").repeat(
                                4
                            ),
                        )
                    }
                }
                IconButton(onClick = {
                    expanded = !expanded
                }) {
                    Icon(
                        imageVector = if (expanded) Filled.KeyboardArrowUp else Filled.KeyboardArrowDown, contentDescription = if (expanded) {
                            stringResource(R.string.show_less)
                        } else {
                            stringResource(R.string.show_more)
                        }
                    )
                }
            }
        }
    }

    @Preview(name = "Light Mode")
    @Preview(uiMode = UI_MODE_NIGHT_YES, showBackground = true, name = "Dark Mode")
    @Composable
    fun DefaultPreview() {
        ComposeAppTheme {
            Surface {
                Greeting(Message("Android"))
            }
        }

    }

    @Composable
    fun Conversation(messages: List<Message>) {
        LazyColumn {
            items(messages) { message ->
                Greeting(message)
            }
        }
    }

    @Composable
    fun MyApp(
        modifier: Modifier = Modifier
    ) {
        var shouldShowOnboarding by rememberSaveable {
            mutableStateOf(true)
        }
//        Surface(modifier) {
        if (shouldShowOnboarding) {
            OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false }, modifier)
        } else {
            Greetings()
        }
//        }
    }

    @Composable
    private fun Greetings(
        modifier: Modifier = Modifier, names: List<String> = List(1000) { "$it" }
    ) {
        LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
            items(names) { name ->
                Greeting(name = name)
            }
        }
    }

    @Composable
    fun OnboardingScreen(onContinueClicked: () -> Unit, modifier: Modifier = Modifier) {
        Column(
            modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text("Welcome to the Basics CodeLab!")
            Button(
                onClick = onContinueClicked, modifier = Modifier
                    .padding(20.dp)
                    .clip(CircleShape)
            ) {
                Text("Continue")
            }
        }
    }

    @Preview(showBackground = true, widthDp = 320, heightDp = 640)
    @Composable
    fun OnboardingScreenPreview() {
        ComposeAppTheme {
            OnboardingScreen(onContinueClicked = {})
        }
    }

    @Preview
    @Composable
    fun MyAppPreview() {
        ComposeAppTheme {
            MyApp(Modifier.fillMaxSize())
        }
    }

    @Preview(
        showBackground = true, widthDp = 320, uiMode = UI_MODE_NIGHT_YES, name = "Dark"
    )
    @Preview(showBackground = true, widthDp = 320)
    @Composable
    fun DefaultGreetingsPreview() {
        ComposeAppTheme {
            Greetings(Modifier.fillMaxSize())
        }
    }

    @Preview(showBackground = true, widthDp = 320)
    @Composable
    fun CenterAlignedTopAppBarExample() {
        Scaffold(topBar = {
            CenterAlignedTopAppBar(
                colors = TopAppBarDefaults.largeTopAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ),
                title = {
                    Text(
                        "Centered Top App Bar", maxLines = 1, overflow = TextOverflow.Ellipsis, color = colorResource(id = R.color.black), fontSize = 22.sp
                    )
                },
                navigationIcon = {
                    IconButton(onClick = { finish() }) {
                        Icon(
                            imageVector = Filled.ArrowBack, contentDescription = "finish"
                        )
                    }
                },
                actions = {
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            imageVector = Filled.Menu, contentDescription = "menu"
                        )
                    }
                },
            )
        }, content = { innerPadding ->
            Content(innerPadding)
        })
    }

    @Preview(showBackground = true)
    @Composable
    fun SmallTopAppBarExample() {
        Scaffold(
            topBar = {
                TopAppBar(colors = TopAppBarDefaults.smallTopAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ), title = {
                    Text("Small Top App Bar")
                })
            },
        ) { innerPadding ->
            Content(innerPadding)
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun MediumTopAppBarExample() {
        val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())

        Scaffold(
            modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
            topBar = {
                MediumTopAppBar(colors = TopAppBarDefaults.mediumTopAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ), title = {
                    Text(
                        "Medium Top App Bar", maxLines = 1, overflow = TextOverflow.Ellipsis
                    )
                }, navigationIcon = {
                    IconButton(onClick = { finish() }) {
                        Icon(
                            imageVector = Filled.ArrowBack, contentDescription = "Localized description"
                        )
                    }
                }, actions = {
                    IconButton(onClick = { }) {
                        Icon(
                            imageVector = Filled.Menu, contentDescription = "Localized description"
                        )
                    }
                }, scrollBehavior = scrollBehavior
                )
            },
        ) { innerPadding ->
            Content(innerPadding)
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun LargeTopAppBarExample() {
        val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(rememberTopAppBarState())

        Scaffold(
            modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
            topBar = {
                LargeTopAppBar(colors = TopAppBarDefaults.largeTopAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ), title = {
                    Text(
                        "Large Top App Bar", maxLines = 1, overflow = TextOverflow.Ellipsis
                    )
                }, navigationIcon = {
                    IconButton(onClick = { finish() }) {
                        Icon(
                            imageVector = Filled.ArrowBack, contentDescription = "Localized description"
                        )
                    }
                }, actions = {
                    IconButton(onClick = { }) {
                        Icon(
                            imageVector = Filled.Menu, contentDescription = "Localized description"
                        )
                    }
                }, scrollBehavior = scrollBehavior
                )
            },
        ) { innerPadding ->
            Content(innerPadding)
        }
    }

    @Preview(showBackground = true)
    @Composable
    fun BottomAppBarExample() {
        Scaffold(
            bottomBar = {
                BottomAppBar(actions = {
                    IconButton(onClick = { /* do something */ }) {
                        Icon(Filled.Check, contentDescription = "Localized description")
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Filled.Edit,
                            contentDescription = "Localized description",
                        )
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Filled.Check,
                            contentDescription = "Localized description",
                        )
                    }
                    IconButton(onClick = { /* do something */ }) {
                        Icon(
                            Filled.Menu,
                            contentDescription = "Localized description",
                        )
                    }
                }, floatingActionButton = {
                    FloatingActionButton(
                        onClick = { /* do something */ },
                        containerColor = BottomAppBarDefaults.bottomAppBarFabColor,
                        elevation = FloatingActionButtonDefaults.bottomAppBarFabElevation()
                    ) {
                        Icon(Filled.Add, "Localized description")
                    }
                })
            },
        ) { innerPadding ->
            Text(
                modifier = Modifier.padding(innerPadding), text = "Example of a scaffold with a bottom app bar."
            )
        }
    }

    @Composable
    private fun Content(innerPadding: PaddingValues) {
        println("innerPadding = $innerPadding")
        LazyColumn(
            contentPadding = PaddingValues(
                innerPadding.calculateStartPadding(LayoutDirection.Ltr),
                innerPadding.calculateTopPadding() + dimensionResource(id = R.dimen.dp_10),
                innerPadding.calculateEndPadding(LayoutDirection.Ltr),
                innerPadding.calculateBottomPadding() + dimensionResource(id = R.dimen.dp_10)
            ), verticalArrangement = Arrangement.spacedBy(0.dp)
        ) {
            val list = (0..100).map { it.toString() }
            items(count = list.size) {
                Card(
                    colors = CardDefaults.cardColors(
                        containerColor = MaterialTheme.colorScheme.primary
                    ), modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
                ) {
                    Text(
                        text = list[it],
                        style = MaterialTheme.typography.bodyLarge,
                        modifier = Modifier
                            .fillMaxWidth()
                            .absolutePadding(top = 10.dp, bottom = 10.dp),
                        textAlign = TextAlign.Center,
                        color = colorResource(R.color.green),
                        fontSize = integerResource(id = R.integer.sp_30).sp
                    )
                }
            }
        }
    }
}